package br.ufrj.dcc.traffictip.helper;

import java.util.Enumeration;
import java.util.Hashtable;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class HtmlParser
{

	public HtmlParser()
	{
		
	}
	
	public NodeList getBoletimList()
	{
		Parser parser;
		
		HasAttributeFilter filterClassImpar = new HasAttributeFilter();
		HasAttributeFilter filterClassPar = new HasAttributeFilter();
		
		filterClassImpar.setAttributeName("class");
		filterClassImpar.setAttributeValue("impar");
		
		filterClassPar.setAttributeName("class");
		filterClassPar.setAttributeValue("par");
		
		OrFilter orfilter = new OrFilter(filterClassImpar,filterClassPar);
		NodeList list = null;
		
		try
		{
			parser = new Parser("http://transito.rio.rj.gov.br/entrada.cfm?pagina=6&menusel=5");
			list = parser.parse(orfilter);
			
			if(list.size() < 1)
				return null;
		} 
		catch (ParserException e) { e.printStackTrace(); }
		
		return list;
	}
	
	public String formatStreet(String street)
	{
		street = street.toLowerCase();
		
		int indexOfComma = street.indexOf(",");
		int indexOfHifen = street.indexOf("-");
		int index = -1;
		
		if( (indexOfComma < 0) && (indexOfHifen > 0))
			index = indexOfHifen;
		else if( (indexOfComma > 0) && (indexOfHifen < 0))
			index = indexOfComma;
		else
			index = indexOfComma < indexOfHifen ? indexOfComma : indexOfHifen;
	
		if(index > 0)
			street = street.substring(0,index);
				
		street = street.trim();
		
		Hashtable< String, String> abrevHash = new Hashtable< String, String>();
		abrevHash.put("rua", "r.");
		

		Enumeration<String> keys = abrevHash.keys();		
		while(keys.hasMoreElements())
		{
			String original = keys.nextElement();
			String replacement =  abrevHash.get(original);
			street = street.replace(original, replacement);
		}
		
		return street;
	}

	public String formatColor(String color)
	{
		color = color.replaceAll("leg","").replaceAll("Maior", "").toLowerCase();
		
		Hashtable< String, String> colorsHash = new Hashtable< String, String>();
		colorsHash.put("verde", "#006600");
		colorsHash.put("amarelo", "#999900");
		colorsHash.put("laranja", "#FF9900");
		colorsHash.put("vermelho", "#FF3300");
		colorsHash.put("cinza", "#000000");
		
		return colorsHash.get(color);
	}
	
	public Hashtable<String, String> extractData(NodeList list)
	{
		Hashtable< String, String> hash = new Hashtable< String, String>(); 
		
		for(int i = 0; i < list.size(); i++)
		{
			NodeList tds = list.elementAt(i).getChildren();

			if(tds.size() < 4)
				return null;
			
			Node tdstreet = tds.elementAt(1);
			Node tdimg = tds.elementAt(3);
			
			if(tdimg.getChildren().size() < 2)
				return null;
			
			Tag img = (Tag) tdimg.getChildren().elementAt(1);
			
			String fields[] = img.getAttribute("src").split("[./]");
			
			if(fields.length < 3)
				return null;
			
			String imgvalue = fields[1];
			String color = this.formatColor(imgvalue);
			String streetname = this.formatStreet(tdstreet.toPlainTextString());
		
			hash.put(streetname, color);
		}
		
		return hash;
	}
	
	public static void main(String[] args)
	{
		Parser parser;
		
		HasAttributeFilter filterClassImpar = new HasAttributeFilter();
		HasAttributeFilter filterClassPar = new HasAttributeFilter();
		
		filterClassImpar.setAttributeName("class");
		filterClassImpar.setAttributeValue("impar");
		
		filterClassPar.setAttributeName("class");
		filterClassPar.setAttributeValue("par");
		
		OrFilter orfilter = new OrFilter(filterClassImpar,filterClassPar);

		try
		{
			parser = new Parser("http://transito.rio.rj.gov.br/entrada.cfm?pagina=6&menusel=5");
			NodeList list = parser.parse(orfilter);
			
			for(int i = 0; i < list.size(); i++)
			{
				NodeList tds = list.elementAt(i).getChildren();

				Node tdstreet = tds.elementAt(1);
				Node tdimg = tds.elementAt(3);
				Tag img = (Tag) tdimg.getChildren().elementAt(1);
				String imgvalue = img.getAttribute("src").split("[./]")[1];
				String color = imgvalue.replaceAll("leg","").replaceAll("Maior", "").toLowerCase();
			
				System.out.println(tdstreet.toPlainTextString().trim() + " - " + color);
				
			}
		
		} 
		catch (ParserException e) { e.printStackTrace(); }
	}	
}
